package com.tanhua.dubbo.api;

import com.tanhua.mongo.Comment;
import com.tanhua.mongo.Movement;
import com.tanhua.myenum.CommentType;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * 评论表api-impl
 */

@DubboService
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Comment> getComments(String movementId, CommentType comment, Integer page, Integer pagesize) {
        Query query = Query.query(
                Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType").is(comment.getType()))
                .skip((page - 1) * pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));

        return mongoTemplate.find(query, Comment.class);
    }

    /**
     * 保存评论，点赞，喜欢并返回最新数据
     * @param comment
     * @return
     */
    @Override
    public Integer save(Comment comment) {
        //为comment设置被评论人id,publishUserId为被评论人即发布人Id
        Movement movement = mongoTemplate.findById(comment.getUserId(), Movement.class);
        if (movement != null) {
            comment.setPublishUserId(movement.getUserId());
        }
        mongoTemplate.save(comment);
        //更新总评论数，并返回最新评论数
        //第一个参数为查询条件，第二个参数为更新及更新字段，第三个参数为更新的设置属性，当前操作类的字节码对象
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if(comment.getCommentType() == CommentType.COMMENT.getType()){
            update.inc("commentCount",1);
        }else if(comment.getCommentType() == CommentType.LIKE.getType()){
            update.inc("likeCount",1);
        }else if(comment.getCommentType() == CommentType.LOVE.getType()){
            update.inc("loveCount",1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        //获取更新的最新参数
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        //返回对应类型的最新数据
        return  modify.statisCount(comment.getCommentType());
    }

    /**
     * 判断mongodb中是否已有点赞或喜欢数据
     * @param movementId
     * @param userId
     * @param commentType
     * @return
     */
    @Override
    public boolean getStatus(String movementId, Long userId, CommentType commentType) {
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(movementId))
                                        .and("commentType").is(commentType.getType())
                                        .and("userId").is(userId));
        Comment comment = mongoTemplate.findOne(query, Comment.class);
        if(comment != null){
            return true;
        }else{
            return false;
        }

    }

    @Override
    public Integer delete(String movementId, Long userId, CommentType commentType) {
        //设置查询条件，删除对应数据
        Query movementQuery = Query.query(Criteria.where("userId").is(userId)
                                    .and("content").is(commentType.getType())
                                    .and("publishId").is(movementId));
        mongoTemplate.remove(movementQuery,Movement.class);
        //更新总评论数，并返回最新评论数
        //第一个参数为查询条件，第二个参数为更新及更新字段，第三个参数为更新的设置属性，当前操作类的字节码对象
        Query query = Query.query(Criteria.where("id").is(movementId));
        Update update = new Update();
        if(commentType == CommentType.COMMENT){
            update.inc("commentCount",-1);
        }else if(commentType == CommentType.LIKE){
            update.inc("likeCount",-1);
        }else if(commentType == CommentType.LOVE){
            update.inc("loveCount",-1);
        }
        //设置更新参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        //获取更新的最新参数
        options.returnNew(true);
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        //返回对应类型的最新数据
        return  modify.statisCount(commentType.getType());
    }
}
